package com.junzhiit.nengji.api.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.serializer.Format;
import com.junzhiit.nengji.api.service.ShopService;
import com.junzhiit.nengji.api.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.api.web.exception.UserNotFoundException;
import com.junzhiit.nengji.api.web.vo.ResultVO;
import com.junzhiit.nengji.api.web.vo.ShopVO;
import com.junzhiit.nengji.bo.ShopBO;
import com.junzhiit.nengji.bo.ShopDetailsBO;
import com.junzhiit.nengji.dao.FavoriteShopDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.ShopHistoryDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.UserEvaluateDAO;
import com.junzhiit.nengji.model.FavoriteShop;
import com.junzhiit.nengji.model.ShopHistory;
import com.junzhiit.nengji.model.enums.ShopHistoryType;

@Service
public class ShopServiceImpl extends BaseServiceImpl implements ShopService {
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired 
	private FavoriteShopDAO favoriteShopDAO;
	
	@Autowired
	private UserEvaluateDAO userEvaluateDAO;
	
	@Autowired
	private ShopHistoryDAO shopHistoryDAO;

	@Override
	public ShopVO getById(String userId,String shopId) {
		
		ShopBO shopBO= shopDAO.findShopById(shopId);
		if (shopBO == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		int count= userDAO.checkMember(userId);
		if (count < 0) {
			throw new UserNotFoundException("用户不存在");
		}
		
		ShopVO shopVO=new ShopVO();
		BeanUtils.copyProperties(shopBO, shopVO);
		
		Double score= userEvaluateDAO.getShopEvaluateScore(shopId);
		shopVO.setScore(score==null?"0":Format.decimalToString(score,Format.DECIMAL_N));
		
		String Images = shopBO.getBannerImages();
		List<String> bannerImages=new ArrayList<String>();
		if (StringUtils.isNotBlank(Images)) {
			bannerImages = Arrays.asList(Images.split(","));
		}
		shopVO.setBannerImages(bannerImages);
		
		FavoriteShop favoriteShop=favoriteShopDAO.getByCondition(userId, shopId);
		if(favoriteShop!=null){
			shopVO.setFavorite(true);
			shopVO.setFavoriteShopId(favoriteShop.getId());
		}else{
			shopVO.setFavorite(false);
		}
		ShopHistory shopHistory = shopHistoryDAO.getTodayByUserId(userId, shopId, ShopHistoryType.VIEW.toIntValue());
		if(shopHistory == null){
			ShopHistory param = new ShopHistory();
			param.setShopId(shopId);
			param.setUserId(userId);
			param.setNumber(1);
			param.setType(ShopHistoryType.VIEW.toIntValue());
			shopHistoryDAO.insert(param);
		}else{
			shopHistory.setNumber(shopHistory.getNumber()+1);
			shopHistoryDAO.update(shopHistory);
		}
		return shopVO;
	}

	@Override
	public ResultVO findShops(String name, String userId) {
		ResultVO result = new ResultVO();
		List<ShopDetailsBO> shopList = shopDAO.findShops(name);
		result.setData(shopList);
		return result;
	}

	@Override
	public ResultVO addShopHistory(String shopId, String userId, Integer type) {
		ResultVO result = new ResultVO();
		ShopHistory shopHistory = shopHistoryDAO.getTodayByUserId(userId, shopId, type);
		if(shopHistory == null){
			ShopHistory param = new ShopHistory();
			param.setShopId(shopId);
			param.setUserId(userId);
			param.setNumber(1);
			param.setType(type);
			shopHistoryDAO.insert(param);
		}else{
			shopHistory.setNumber(shopHistory.getNumber()+1);
			shopHistoryDAO.update(shopHistory);
		}
		return result;
	}
	
}
